package com.android.namecard.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.namecard.R;
import com.android.namecard.entity.Namecard;
import com.android.namecard.model.NamecardDBHelper;

public class PreviewActivity extends Activity{
	
	AbsoluteLayout cv_front;
	AbsoluteLayout cv_back;
	Bitmap front_image;
	Bitmap back_image;
	
	private TextView name;
	private TextView career;
	private TextView company;
	private TextView address;
	private TextView phone;
	private TextView email;
	private TextView qq;
	private TextView website;
	private TextView msn;
	private TextView business;
	private Namecard namecard = new Namecard();
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.preview);
        
        getData();
        setTextView();
        
        
        Button save_button = (Button)findViewById(R.id.preview_save);
        Button return_button = (Button)findViewById(R.id.preview_return);
        //保存按钮
        save_button.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				save();
			}
        });
        //返回按钮
        return_button.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				PreviewActivity.this.finish();
			}
        });
    }
    
    private void save(){
    	cv_front = (AbsoluteLayout)findViewById(R.id.preview_front);
		cv_back = (AbsoluteLayout)findViewById(R.id.preview_back);
        front_image = Bitmap.createBitmap(445, 249, Bitmap.Config.RGB_565);
        cv_front.draw(new Canvas(front_image));
        back_image = Bitmap.createBitmap(445, 249, Bitmap.Config.RGB_565);
        cv_back.draw(new Canvas(back_image));
        
        NamecardDBHelper namecardDB = new NamecardDBHelper(getBaseContext());
        namecardDB.insertNamecard(namecard.getName(), namecard.getCareer(), namecard.getCompany(), 
        	 namecard.getAddress(), namecard.getEmail(), namecard.getPhone(), namecard.getQq(), 
        	 namecard.getMsn(), namecard.getWebsite(), namecard.getBusiness(), NamecardDBHelper.PRIVATE_TYPE,
        	 front_image, back_image);
    }
    
    private void getData(){
    	Bundle bundle = this.getIntent().getExtras();
    	namecard.setName(bundle.getString(NamecardDBHelper.NAME));
    	namecard.setCareer(bundle.getString(NamecardDBHelper.CAREER));
    	namecard.setCompany(bundle.getString(NamecardDBHelper.COMPANY));
    	namecard.setAddress(bundle.getString(NamecardDBHelper.ADDRESS));
    	namecard.setPhone(bundle.getString(NamecardDBHelper.PHONE));
    	namecard.setEmail(bundle.getString(NamecardDBHelper.EMAIL));
    	namecard.setQq(bundle.getString(NamecardDBHelper.QQ));
    	namecard.setWebsite(bundle.getString(NamecardDBHelper.WEBSITE));
    	namecard.setMsn(bundle.getString(NamecardDBHelper.MSN));
    	namecard.setBusiness(bundle.getString(NamecardDBHelper.BUSINESS));
    }
    
    private void setTextView(){
    	name = (TextView)findViewById(R.id.preview_name);
    	career = (TextView)findViewById(R.id.preview_career);
    	company = (TextView)findViewById(R.id.preview_company);
    	address = (TextView)findViewById(R.id.preview_address);
    	phone = (TextView)findViewById(R.id.preview_phone);
    	email = (TextView)findViewById(R.id.preview_email);
    	qq = (TextView)findViewById(R.id.preview_qq);
    	website = (TextView)findViewById(R.id.preview_website);
    	
    	/*
    	name.setText(bundle.getString(NamecardDBHelper.NAME));
    	career.setText(bundle.getString(NamecardDBHelper.CAREER));
    	company.setText("公司:"+bundle.getString(NamecardDBHelper.COMPANY));
    	address.setText("地址:"+bundle.getString(NamecardDBHelper.ADDRESS));
    	phone.setText("电话:"+bundle.getString(NamecardDBHelper.PHONE));
    	email.setText("Email:"+bundle.getString(NamecardDBHelper.EMAIL));
    	qq.setText("QQ:"+bundle.getString(NamecardDBHelper.QQ));
    	website.setText("网址:"+bundle.getString(NamecardDBHelper.WEBSITE));
    	*/
    	
    	name.setText("王三号");
    	career.setText("学生");
    	company.setText("公司:华南理工大学");
    	address.setText("地址:广州市番禺区大学城");
    	phone.setText("电话:13416134758");
    	email.setText("Email:416180381@qq.com");
    	qq.setText("QQ:416180381");
    	website.setText("网址:http://www.baidu.com");
    	
    }
}
